package com.example.service;

import com.example.dto.MessageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Slf4j
public class FanoutProducerService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发送广播消息
     * @param message 消息内容
     */
    public void sendFanoutMessage(String message) {
        // 广播模式下，routingKey 参数会被忽略，可以传空字符串
        String fullMessage = "[" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "] " + message;
        rabbitTemplate.convertAndSend("fanout.exchange", "", fullMessage);
        log.info("已发送广播消息: {}", fullMessage);
    }

    /**
     * 发送广播消息 - 带对象
     * @param object 消息对象
     */
    public void sendFanoutMessage(Object object) {
        rabbitTemplate.convertAndSend("fanout.exchange.obj", "", object);
        log.info("已发送广播消息对象: {}", object);
    }
}